/**
 * 
 */
package com.zxc.mongodb;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.alibaba.fastjson.JSONObject;
import com.mongodb.Block;
import com.mongodb.MongoClient;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import com.zxc.jdbc.JdbcDemo;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.RandomUtil;

/**
 * @author Administrator
 *
 */
public class CarrierFetchDemo {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		// 获取链接    
        MongoClient mongoClient = new MongoClient("192.168.100.10", 27017);   
        
        // 获取数据库    
        MongoDatabase database = mongoClient.getDatabase("base_info");    
        // 进入某个文档集    
        MongoCollection<org.bson.Document> collection = database.getCollection("carriers");   
        
        MongoCollection<org.bson.Document> errorColl = database.getCollection("carriers_error");  
		
        List<String> errors = new ArrayList<String>();
		
//        String one = "1234567890QWERTYUIOPASDFGHJKLZXCVBNM";
//		String two = "1234567890QWERTYUIOPASDFGHJKLZXCVBNM";
//		
//		for (int i = 0; i < one.length(); i++) {
//			for (int j = 0; j < two.length(); j++) {
//				String code = ""+one.charAt(i)+two.charAt(j);
//				try {
//					JSONObject data = fetchData(code);
//					if(MapUtil.isNotEmpty(data) && data.containsKey("Code")) {
//						org.bson.Document doc = new org.bson.Document();
//						doc.putAll(data);
//		        		collection.insertOne(doc);
//					}else if(MapUtil.isNotEmpty(data) && data.containsKey("error")){
//						org.bson.Document doc = new org.bson.Document();
//						doc.put("Code", code);
//						doc.putAll(data);
//						errorColl.insertOne(doc);
//					}
//				} catch (Exception e) {
//					errors.add(code);
//				}
//				
//				try {
//					int dd = RandomUtil.randomInt(1, 6);
//					Thread.sleep(dd * 1000);
//				} catch (InterruptedException e) {
//				}
//			}
//		}
		
//		String errorcodes = "1Q,1W,1E,1R,1T,1Y,1U,1I,1O,1P,1A,1D,1F,1G,1H,1J,1K,1L,1Z,1X,1C,1V,1B,1N,1M,2W,2E,2R,2U,2O,2A,2F,2Z,2X,2V,2N,3R,3T,3Y,3I,3D,3F,3Z,4R,4P,4A,4F,4J,4K,4V,5E,5I,5G,5L,6Q,6T,6Y,6I,6O,6P,6D,6F,6G,6K,6Z,6X,6V,6N,7E,7Y,7O,7S,7K,7X,7B,7M,8W,8T,8Y,8O,8G,8J,8X,8N,9Q,9O,9P,9F,9G,9L,9Z,9B,0Q,0W,0E,0R,0T,0Y,0U,0I,0O,0P,0A,0S,0D,0F,0G,0H,0K,0L,0Z,0X,0C,0V,0B,0N,0M,Q1,Q3,Q4,Q5,Q6,Q7,Q9,Q0,W1,W4,W9,W0,WV,E1,E2,E8,E0,R6,R7,R8,R9,R0,T1,T4,T6,Y1,Y3,Y6,Y0,YY,YI,U1,U4,U5,U0,I1,I3,I5,I6,I7,I0,O1,O5,O7,O0,P1,P2,P8,A1,A7,A8,S1,S5,S6,S0,D1,F1,F2,F6,F0,G1,G2,G6,G0,H1,H3,H4,H6,H7,H8,H0,HJ,J1,J3,J5,J6,J7,J0,JN,K1,K2,K5,K8,K0,KT,L1,L4,L8,L9,L0,Z1,Z3,Z5,Z9,Z0,ZJ,ZX,X1,X2,X5,X6,X9,X0,XO,XG,XX,XN,C1,C4,C6,C0,V1,V5,V6,VP,VH,VM,B1,B4,N1,N3,N5,N6,N0,M1,M8,M0";
//		String[] arr = StringUtils.split(errorcodes,",");
//		for (String code : arr) {
//			try {
//				JSONObject data = fetchData(code);
//				if(MapUtil.isNotEmpty(data) && data.containsKey("Code")) {
//					org.bson.Document doc = new org.bson.Document();
//					doc.putAll(data);
//	        		collection.insertOne(doc);
//				}else if(MapUtil.isNotEmpty(data) && data.containsKey("error")){
//					org.bson.Document doc = new org.bson.Document();
//					doc.put("Code", code);
//					doc.putAll(data);
//					errorColl.insertOne(doc);
//				}
//			} catch (Exception e) {
//				e.printStackTrace();
//				errors.add(code);
//			}
//			
//			try {
//				int dd = RandomUtil.randomInt(1, 6);
//				Thread.sleep(dd * 1000);
//			} catch (InterruptedException e) {
//			}
//		}
		
        collection.find().forEach(new Block<org.bson.Document>() {
        	
			@Override
			public void apply(org.bson.Document t) {
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("prefix", t.get("Prefix"));
				map.put("code", t.get("Code"));
				map.put("name", t.get("Name"));
				try {
					JdbcDemo.insertAirlines(map);
				} catch (SQLException | InterruptedException e) {
					e.printStackTrace();
				}
			}
		});
        
		System.out.println(StringUtils.join(errors, ","));
		mongoClient.close();
	}

	
	public static JSONObject fetchData(String code) throws IOException{
		Document doc = null;
		doc = Jsoup.connect("https://www.seabay.cn/cn/airlines/"+code+".html").get();
		Element divmessage = doc.getElementById("divmessage");
		
		Elements tables = divmessage.getElementsByTag("table");
		Element tbody = tables.first().child(0);
		
		if(tbody.childNodeSize() > 1 && !tbody.html().contains("没有您查询的结果")) {
			Element tr = tbody.child(1);
			
			JSONObject data = new JSONObject();
			if(CollectionUtil.isNotEmpty(tr.child(0).getElementsByTag("strong"))) {
				data.fluentPut("Prefix", tr.child(0).getElementsByTag("strong").first().child(0).html());
				data.fluentPut("Code", tr.child(1).getElementsByTag("strong").first().child(0).html());
				data.fluentPut("Name", tr.child(2).getElementsByTag("strong").first().child(0).child(0).html());
			}else {
				data.fluentPut("error", tr.html());
			}
			return data;
		}
		return null;
	}
}
